package com.sanpu.kxe.business.main.presenter;

import android.app.Activity;

import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.bean.UserConfigOutput;
import com.sanpu.kxe.business.main.contract.SetPayPwdSecondContract;
import com.sanpu.kxe.business.main.model.MainModel;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.utils.ToastUtil;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

import static android.app.Activity.RESULT_OK;

/**
 * Description：设置支付密码第二步
 * Created by 薛勇军 on 2019/3/20.
 */

public class SetPayPwdSecondPresenter implements SetPayPwdSecondContract.Presenter {

    private SetPayPwdSecondContract.View mView;

    private CompositeSubscription mSubscription;

    public SetPayPwdSecondPresenter(SetPayPwdSecondContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void onSetPayPwd(String payPwd) {
        String accountName = "";
        UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            accountName = centerOutput.getUserName();
        }
        mView.showLoadingDialog(true);
        Subscription subscription = MainModel.getInstance().setPayPassword(payPwd, accountName)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<String>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                        mView.clearPwd();
                    }

                    @Override
                    public void onNext(String s) {
                        ToastUtil.showTextViewPrompt("设置支付密码成功");
                        UserConfigOutput configOutput = CacheManage.getInstance().getCache(CacheModel.USER_CONFIG);
                        if (configOutput != null) {
                            configOutput.setPayPwd("1");
                        }
                        CacheManage.getInstance().saveCache(CacheModel.USER_CONFIG, configOutput);
                        ((Activity)mView.getContext()).setResult(RESULT_OK);
                        ((Activity)mView.getContext()).finish();
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
